package qdb

import (
	"context"
	"fmt"
	_ "gfrest/common/test_util"
	"net"
	"net/http"
	"strings"
	"time"

	"github.com/elastic/go-elasticsearch/v8"
	"github.com/gogf/gf/frame/g"
	"github.com/qiniu/qmgo"
)

var CollTxBlack *qmgo.Collection
var EsClient *elasticsearch.Client

func init() {
	ctx := context.Background()
	minpsize := uint64(4)

	var (
		client *qmgo.Client
		err    error
	)
	mongodbUrl := g.Config().GetString("mongodbUrl", "")
	if mongodbUrl == "" {
		fmt.Println("no mongodbUrl!")
		return
	}
	client, err = qmgo.NewClient(ctx, &qmgo.Config{Uri: mongodbUrl, MinPoolSize: &minpsize})

	if err != nil {
		g.Log().Error(err)
	}
	db := client.Database("anquan")
	CollTxBlack = db.Collection("txblack")

	// es
	var Addresses = strings.Split(g.Config().GetString("EsAddresses", ""), ";")
	cfg := elasticsearch.Config{
		Addresses: Addresses,
		Transport: &http.Transport{
			MaxIdleConnsPerHost:   10,
			ResponseHeaderTimeout: time.Second,
			DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
		},
	}

	EsClient, err = elasticsearch.NewClient(cfg)
	if err != nil {
		g.Log().Printf("Error creating the client: %s", err)
	}

}
